package com.pb.wkflow.core.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 工作流本地线程变量，用于传递给事件监听
 * @author 苗益辉
 * @date 2022/9/26 13:55
 */
public class WorkflowThreadLocalUtil {

    private static final ThreadLocal<Map> localMap = new ThreadLocal<Map>() {
        @Override
        protected Map initialValue() {
            return new HashMap(2);
        }
    };

    public static void put(String key, String value) {
        localMap.get().put(key, value);
    }

    public static void put(String key, Object value) {
        localMap.get().put(key, value);
    }

    public static Object get(String key) {
        return localMap.get().get(key);
    }

    public static String getString(String key) {
        Object o = localMap.get().get(key);
        if (o instanceof String) {
            return (String) o;
        }
        return null;
    }

    public static void removeAll() {
        localMap.remove();
    }

    public static void remove(String key){
        localMap.get().remove(key);
    }
}
